﻿// Json contendo todas as setas possíveis.
// A seta S3 está junto com as S2, pois comportamento será o mesmo para os dois
var JsonSetas = {
  S1: [
    { "id": "0", "texto": "Sem Seta" },
    { "id": "1", "texto": "Direita" },
    { "id": "2", "texto": "Esquerda" },
    { "id": "3", "texto": "Frente" },
    { "id": "4", "texto": "Oblíqua à Direita" },
    { "id": "5", "texto": "Oblíqua à Esquerda" },
  ],
  S2: [
    { "id": "0", "texto": "Sem Seta" },
    { "id": "8", "texto": "Frente" },
    { "id": "9", "texto": "Oblíqua à Direita" },
    { "id": "10", "texto": "Oblíqua à Esquerda" },
    { "id": "13", "texto": "Direita" },
    { "id": "14", "texto": "Esquerda" }
  ]
};

// Este código será executado sempre que a página carregar
$(document).ready(function () {
  CarregaSlider();

  // Preenche os dropdowns com os dados do Json
  $.each(JsonSetas.S1, function (key, value) {
    $('#ddlSeta1').append($('<option></option>').val(value.id).html(value.texto));
    $('#ddlSeta2').append($('<option></option>').val(value.id).html(value.texto));
    $('#ddlSeta3').append($('<option></option>').val(value.id).html(value.texto));
    $('#ddlSeta4').append($('<option></option>').val(value.id).html(value.texto));
  });
  InitiateFields();
  $('#chkAgruparSetas12').click(function () {
    CallAllFunctions(12, 23, 34);
    CallAllFunctions(23, 12, 34);
    CallAllFunctions(34, 12, 23);
    ReplaceData(this.checked ? true : false, 1, 2);
  });
  $('#chkAgruparSetas23').click(function () {
    CallAllFunctions(12, 23, 34);
    CallAllFunctions(23, 12, 34);
    CallAllFunctions(34, 12, 23);
    ReplaceData(this.checked ? true : false, 2, 3);
  });
  $('#chkAgruparSetas34').click(function () {
    CallAllFunctions(12, 23, 34);
    CallAllFunctions(23, 12, 34);
    CallAllFunctions(34, 12, 23);
    ReplaceData(this.checked ? true : false, 3, 4);
  });

  $('.ddlCores').click(function () {
    var chk12 = document.getElementById('chkAgruparSetas12');
    var chk23 = document.getElementById('chkAgruparSetas23');
    var chk34 = document.getElementById('chkAgruparSetas34');

    if (chk12.checked)
      CheckColors(12, 23, 34);
    if (chk23.checked)
      CheckColors(23, 12, 34);
    if (chk34.checked)
      CheckColors(34, 12, 23);
  });
  $('#btnGerar').click(function (e) {
    CheckFields();
  });
});

// utiliza o plugin BlockUI
function blockScreen() {
  $.blockUI({
    message: '<div style="font-size: 30px;padding: 20px;">Aguarde...</div>'
  });
}
// finaliza a execução do plugin BlockUI
function unblockScreen() {
  $.unblockUI();
}

// função para criar o slider
function CarregaSlider() {
  $('#slider img:eq(0)').addClass('active').show();
  var interval = setInterval(slide, 10000);


  $('#sliderPrevious').click(function () {
    clearInterval(interval);
    interval = setInterval(slide, 10000);
    if ($('.active').prev().size()) {
      $('.active').fadeOut().removeClass('active').prev().fadeIn().addClass('active');
    } else {
      $('.active').fadeOut().removeClass('active');
      $('#slider img:last-child').fadeIn().addClass('active');
    }
  });

  $('#sliderNext').click(function () {
    clearInterval(interval);
    interval = setInterval(slide, 10000);
    if ($('.active').next().size()) {
      $('.active').fadeOut().removeClass('active').next().fadeIn().addClass('active');
    } else {
      $('.active').fadeOut().removeClass('active');
      $('#slider img:eq(0)').fadeIn().addClass('active');
    }
  });

}
function slide() {
  if ($('.active').next().size()) {
    $('.active').fadeOut().removeClass('active').next().fadeIn().addClass('active');
  } else {
    $('.active').fadeOut().removeClass('active');
    $('#slider img:eq(0)').fadeIn().addClass('active');
  }

}

// Caso seja postback, recupera os dados da execução anterior e atualizam a tela
function InitiateFields() {
  try {
    var dropdown;
    var hdfSeta1 = document.getElementById('hdfSetaLinha1');
    var hdfSeta2 = document.getElementById('hdfSetaLinha2');
    var hdfSeta3 = document.getElementById('hdfSetaLinha3');
    var hdfSeta4 = document.getElementById('hdfSetaLinha4');

    if (hdfSeta1 != null) {
      var tbl1 = document.getElementById("tbl1");
      if ((tbl1 != null) && (tbl1.getElementsByTagName('tr')[2].getElementsByTagName('td').length > 3)) {
        tbl1.style.opacity = 1;
      }
    }

    if (hdfSeta2 != null) {
      var tbl2 = document.getElementById("tbl2");
      if ((tbl2 != null) && (tbl2.getElementsByTagName('tr')[2].getElementsByTagName('td').length > 3)) {
        tbl2.style.opacity = 1;
      }
    }

    if (hdfSeta3 != null) {
      var tbl3 = document.getElementById("tbl3");
      if ((tbl3 != null) && (tbl3.getElementsByTagName('tr')[2].getElementsByTagName('td').length > 3)) {
        tbl3.style.opacity = 1;
      }
    }
    if (hdfSeta4 != null) {
      var tbl4 = document.getElementById("tbl4");
      if ((tbl4 != null) && (tbl4.getElementsByTagName('tr')[2].getElementsByTagName('td').length > 3)) {
        tbl4.style.opacity = 1;
      }
    }

    // procura nos dropdowns os elementos que estavam na tela anterior, e se os encontrar,
    // atualiza seu valor
    $.each(JsonSetas.S1, function (index, value) {
      if (hdfSeta1 != null) {
        if (hdfSeta1.value == value.id) {
          var ddlSeta1 = document.getElementById('ddlSeta1');
          ddlSeta1.value = value.id;
        }
      }
      if (hdfSeta2 != null) {
        if (hdfSeta2.value == value.id) {
          var ddlSeta2 = document.getElementById('ddlSeta2');
          ddlSeta2.value = value.id;
        }
      }
      if (hdfSeta3 != null) {
        if (hdfSeta3.value == value.id) {
          var ddlSeta3 = document.getElementById('ddlSeta3');
          ddlSeta3.value = value.id;
        }
      }
      if (hdfSeta4 != null) {
        if (hdfSeta4.value == value.id) {
          var ddlSeta4 = document.getElementById('ddlSeta4');
          ddlSeta4.value = value.id;
        }
      }
    });
    $.each(JsonSetas.S2, function (index, value) {
      if (hdfSeta1 != null) {
        if (hdfSeta1.value == value.id) {
          var ddlSeta1 = document.getElementById('ddlSeta1');
          ddlSeta1.value = value.id;
        }
      }
      if (hdfSeta2 != null) {
        if (hdfSeta2.value == value.id) {
          var ddlSeta2 = document.getElementById('ddlSeta2');
          ddlSeta2.value = value.id;
        }
      }
      if (hdfSeta3 != null) {
        if (hdfSeta3.value == value.id) {
          var ddlSeta3 = document.getElementById('ddlSeta3');
          ddlSeta3.value = value.id;
        }
      }
      if (hdfSeta4 != null) {
        if (hdfSeta4.value == value.id) {
          var ddlSeta4 = document.getElementById('ddlSeta4');
          ddlSeta4.value = value.id;
        }
      }
    });

    var chkAgruparSetas12 = document.getElementById('chkAgruparSetas12');
    var chkAgruparSetas23 = document.getElementById('chkAgruparSetas23');
    var chkAgruparSetas34 = document.getElementById('chkAgruparSetas34');

    if (chkAgruparSetas12.checked) {
      ReplaceData(true, 1, 2);
      CheckColors(12, 23, 34);
      var ddl1 = document.getElementById('ddlSeta1');
      for (var i = ddl1.options.length; i-- > 0;) {
        if (ddl1.options[i].value == hdfSeta1.value) {
          ddl1.selectedIndex = i;
          break;
        }
      }

      $('#ddlSeta2').prop('disabled', 'disabled');
    }
    if (chkAgruparSetas23.checked) {
      CheckColors(23, 12, 34);
      ReplaceData(true, 2, 3);
      var ddl2 = document.getElementById('ddlSeta2');

      for (var i = ddl2.options.length; i-- > 0;) {
        if (ddl2.options[i].value == hdfSeta2.value) {
          ddl2.selectedIndex = i;
          break;
        }
      }

      $('#ddlSeta3').prop('disabled', 'disabled');
    }
    if (chkAgruparSetas34.checked) {
      ReplaceData(true, 3, 4);
      CheckColors(34, 12, 23);

      var ddl3 = document.getElementById('ddlSeta3');

      for (var i = ddl3.options.length; i-- > 0;) {
        if (ddl3.options[i].value == hdfSeta3.value) {
          ddl3.selectedIndex = i;
          break;
        }
      }

      $('#ddlSeta4').prop('disabled', 'disabled');
    }
  } catch (e) {
    alert(e.message);
  }
}
function CallAllFunctions(thisNum, nextNum, postNum) {
  CheckAvailableGrouping(thisNum, nextNum, postNum);
  CheckColors(thisNum, nextNum, postNum);
  CheckSetas(thisNum, nextNum, postNum);
}
function ReplaceData(isChecked, componentNumber, nextComponentNumber) {
  try {
    if (isChecked) {
      $('#ddlSeta' + componentNumber).empty();
      $.each(JsonSetas.S2, function (index, value) {
        $('#ddlSeta' + componentNumber).append($('<option></option>').val(value.id).html(value.texto));
      });
      $('#ddlSeta' + nextComponentNumber).addClass('ddlDisabled');
      $('#ddlSeta' + nextComponentNumber).prop('disabled', true);
    } else {
      $('#ddlSeta' + componentNumber).empty();
      $.each(JsonSetas.S1, function (index, value) {
        $('#ddlSeta' + componentNumber).append($('<option></option>').val(value.id).html(value.texto));
      });
      $('#ddlSeta' + nextComponentNumber).removeClass('ddlDisabled');
      $('#ddlSeta' + nextComponentNumber).prop('disabled', false);
    }
  } catch (e) {
    alert(e.message);
    $.unblockUI();
  }
}
function CheckAvailableGrouping(thisGrouping, firstGrouping, secondGrouping) {
  try {
    var thisG = document.getElementById('chkAgruparSetas' + thisGrouping);
    var firstG = document.getElementById('chkAgruparSetas' + firstGrouping);
    var secondG = document.getElementById('chkAgruparSetas' + secondGrouping);

    if (thisG.checked && firstG.checked) {
      var chkAgruparSetas = document.getElementById('chkAgruparSetas' + secondGrouping);
      chkAgruparSetas.check = false;
      chkAgruparSetas.disabled = true;
    } else {
      var chkAgruparSetas = document.getElementById('chkAgruparSetas' + secondGrouping);
      chkAgruparSetas.disabled = false;
    }

    if (thisG.checked && secondG.checked) {
      var chkAgruparSetasII = document.getElementById('chkAgruparSetas' + firstGrouping);
      chkAgruparSetasII.check = false;
      chkAgruparSetasII.disabled = true;
    } else {
      var chkAgruparSetasII = document.getElementById('chkAgruparSetas' + firstGrouping);
      chkAgruparSetasII.disabled = false;
    }
  } catch (e) {
    alert("CheckAvailableGrouping " + e.message);
  }
}
function CheckColors(thisGrouping, firstGrouping, secondGrouping) {
  try {
    var thisG = document.getElementById('chkAgruparSetas' + thisGrouping);
    var firstG = document.getElementById('chkAgruparSetas' + firstGrouping);
    var secondG = document.getElementById('chkAgruparSetas' + secondGrouping);

    if (thisG.checked) {
      var dropdown = document.getElementById('ddlCor' + thisGrouping.toString().charAt(0));
      var nextDropdown = document.getElementById('ddlCor' + thisGrouping.toString().charAt(1));
      $('#ddlCor' + thisGrouping.toString().charAt(1)).addClass('ddlDisabled').prop('disabled', 'disabled');
      nextDropdown.selectedIndex = dropdown.selectedIndex;
    } else {
      $('#ddlCor' + thisGrouping.toString().charAt(1)).removeClass('ddlDisabled').prop('disabled', false);
    }
  } catch (e) {
    alert(e.message);
  }
}
function CheckSetas(thisGrouping, firstGrouping, secondGrouping) {
  try {
    var thisG = document.getElementById('chkAgruparSetas' + thisGrouping);
    var firstG = document.getElementById('chkAgruparSetas' + firstGrouping);
    var secondG = document.getElementById('chkAgruparSetas' + secondGrouping);

    if (thisG.checked) {
      var ddlSeta = document.getElementById('ddlSeta' + thisGrouping.toString().charAt(1));
      $('#ddlSeta' + thisGrouping.toString().charAt(1))
        .addClass('ddlDisabled').prop('selectedIndex', 0).prop('disabled', true);
    } else {
      $('#ddlSeta' + thisGrouping.toString().charAt(1)).removeClass('ddlDisabled');
      $('#ddlSeta' + thisGrouping.toString().charAt(1)).prop('disabled', false);
    }
  } catch (e) {
    alert(e.message);
  }
}


function CheckFields() {
  try {
    blockScreen();

    for (var iLinhas = 1; iLinhas < 5; iLinhas++) {
      var textbox = document.getElementById('txtMensagem' + iLinhas);

      if (textbox.value != "") {
        var dropdown = document.getElementById('ddlSeta' + iLinhas);
        $('#hdfSetaLinha' + iLinhas).val(dropdown.value);
      }
      else {
        $('#hdfSetaLinha' + iLinhas).val(0);
      }
    }
    unblockScreen();
  } catch (e) {
    unblockScreen();
    alert("CheckFields " + e.message);
  }
}